package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 执行查看商品详情和分页查询商品列表以及查询推荐商品的业务
 * 创建日期: 2022/04/22 22:29
 *
 * @author Administrator
 */
@RestController
public class HXShoppingController {
    @Autowired
    IProductService iProductService;

    @GetMapping("/shopping/product/{id}")
    public ResponseData getProductDetail(@PathVariable("id") Long id) {
        // 将请求传来的参数封装到request对象里面
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);

        ProductDetailResponse productDetail = iProductService.getProductDetail(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())) {
            return new ResponseUtil().setData(productDetail.getProductDetailDto());

        }
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());

    }

    @GetMapping("/shopping/goods")
    public ResponseData getGoodsListByPage(@RequestParam Map map) {
        String page = (String) map.get("page");
        String size = (String) map.get("size");
        String sort = (String) map.get("sort");
        String cid = (String) map.get("cid");
        String priceGt = (String) map.get("priceGt");
        String priceLte = (String) map.get("priceLte");
        AllProductRequest request = new AllProductRequest();
        request.setPage(Integer.valueOf(page));
        request.setSize(Integer.valueOf(size));
        if (cid != null) {
            request.setCid(Long.valueOf(cid));
        }
        if (!"".equals(sort)) {
            request.setSort(sort);
        }
        if (!"".equals(priceGt)) {
            request.setPriceGt(Integer.valueOf(priceGt));
        }
        if (!"".equals(priceLte)) {
            request.setPriceLte(Integer.valueOf(priceLte));
        }
        AllProductResponse allProduct = iProductService.getAllProduct(request);


        if (ShoppingRetCode.SUCCESS.getCode().equals(allProduct.getCode())) {
            return new ResponseUtil().setData(allProduct);

        }
        return new ResponseUtil().setErrorMsg(allProduct.getMsg());
    }

    @GetMapping("/shopping/recommend")
    public ResponseData getRecommendGoods() {

        RecommendResponse recommendGoods = iProductService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(recommendGoods.getCode())) {
            return new ResponseUtil().setData(recommendGoods.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(recommendGoods.getMsg());
    }
}
